package com.mooer.utils.ui;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.Toast;

import com.mooer.R;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-12 上午07:24:04 类说明
 */
public class UiUtils {

	/**
	 * 用于UI界面中button的初始化
	 * 
	 * @param v
	 *            初始化的button
	 * @return OnPreDrawListener对象
	 */
	public static OnPreDrawListener initButtonState(final View v) {
		OnPreDrawListener op = new OnPreDrawListener() {

			@Override
			public boolean onPreDraw() {
				v.setSelected(true);
				return true;
			}
		};
		v.getViewTreeObserver().addOnPreDrawListener(op);
		return op;
	}

	/**
	 * 用于切换UI界面中button的状态图
	 * 
	 * @param vs
	 *            牵扯到要改变状态所有button的list
	 * @param v
	 *            当前点击的button
	 * @param op
	 *            初始化的view的OnPreDrawListener对象
	 */
	public static void buttonStateChange(List<View> vs, View v,
			OnPreDrawListener op) {
		for (View view : vs) {
			view.getViewTreeObserver().removeOnPreDrawListener(op);
			view.setSelected(false);
		}
		v.setSelected(true);
	}

	/**
	 * 
	 * @param resources
	 * @param star
	 *            星星的个数
	 * @return
	 */
	public static Drawable selectStar(Resources resources, double star) {
		Drawable d = null;
		if (star == 0) {
			d = resources.getDrawable(R.drawable.value_01);
		} else if (star <= 1) {
			d = resources.getDrawable(R.drawable.value_02);
		} else if (star <= 2) {
			d = resources.getDrawable(R.drawable.value_03);
		} else if (star <= 3) {
			d = resources.getDrawable(R.drawable.value_04);
		} else if (star <= 4) {
			d = resources.getDrawable(R.drawable.value_05);
		} else if (star <= 5) {
			d = resources.getDrawable(R.drawable.value_06);
		}
		return d;
	}

	public static void showToast(Context ctx, String content) {
		Toast.makeText(ctx, content, Toast.LENGTH_SHORT).show();
	}

	public static AlertDialog createAlertDidalog(Context ctx, String title,
			CharSequence[] items) {
		AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
		builder.setTitle(title);
		builder.setSingleChoiceItems(items, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {

					}
				});
		AlertDialog alert = builder.create();
		return alert;
	}

}
